package com.jadefashion.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.mapper.RoleMapper;
import com.jadefashion.pojo.Role;
import com.jadefashion.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    //角色分页数据展示
    @Override
    public Map queryPage(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询角色数据
        List<Role> list = roleMapper.queryPage(queryPageBean.getQueryString());
        //将数据放入pageInfo
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        //创建map集合
        Map map = new HashMap();
        map.put("rows",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }


    //添加角色信息
    @Override
    public void addRole(Role role, List<Integer> permissionIds, List<Integer> menuIds) {
        //添加角色信息
        roleMapper.addRole(role);
        //获取回显的角色id
        Integer id = role.getId();

        //添加角色与菜单的中间表信息
        for (Integer menuId : menuIds) {
            roleMapper.addRoleAndMenu(id,menuId);
        }
        //添加角色与权限的中间表信息
        for (Integer permissionId : permissionIds) {
            roleMapper.addRoleAndPer(id,permissionId);
        }
    }


    //  TODO:  更新角色对应权限数据
    @Override
    public void updatePermissionForRoleByDetail(List<Integer> deleteIds, List<Integer> addIds, Integer currentRowId) {
        //执行删除
        if (deleteIds != null && deleteIds.size() > 0) {
            roleMapper.deletePermissionForRoleByDetail(deleteIds, currentRowId);
        }
        //执行添加
        if (addIds != null && addIds.size() > 0) {
            roleMapper.addRoleForPermissionDetail(addIds, currentRowId);
        }
    }

    //  TODO:  更新角色对应菜单数据
    @Override
    public void updateMenuForRoleByDetail(List<Integer> deleteIds, List<Integer> addIds, Integer currentRowId) {
        //执行删除
        if (deleteIds != null && deleteIds.size() > 0) {
            roleMapper.deleteMenuForRoleByDetail(deleteIds, currentRowId);
        }
        //执行添加
        if (addIds != null && addIds.size() > 0) {
            roleMapper.addRoleForMenuDetail(addIds, currentRowId);
        }
    }

    //TODO: 修改role
    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }

    //TODO: 删除角色
    @Override
    public void deleteRole(Integer id) {
        //查询此role id是否被user引用
        Integer count = roleMapper.queryRoleIdForUserDetail(id);
        if (count > 0) {
            //存在即被引用,抛出异常
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }

        //删除角色对应泉下你中间表数据详情
        roleMapper.deleteRolePermissionDetail(id);

        //删除角色对应菜单中间表数据
        roleMapper.deleteMenuAndRole(id);

        //删除角色
        roleMapper.deleteRole(id);
    }

    //TODO: 查询所有role
    @Override
    public List<Role> queryAllRole() {
        return roleMapper.queryAllRole();
    }

    //TODO: 查询user对应role
    @Override
    public List<Role> queryRoleForUser(Integer id) {
        return roleMapper.queryRoleForUserId(id);
    }

}
